<?

function __autoload($klasa){
	if(file_exists("lib/class.{$klasa}.php")) require_once("lib/class.{$klasa}.php");
	elseif(file_exists("../lib/class.{$klasa}.php")) require_once("../lib/class.{$klasa}.php");
}

/*** GENERAL FUNCTIONS ****************************************************************************************/
function textcut($txt,$len=32){ 
	if ($len<=10){ // sentences
		$sentArr=explode('.',$txt);
		if (!empty($sentArr)) for ($i=0; $i<$len; $i++) $sentences.=$sentArr[$i].'. ';
		return $sentences;
	}
	else { // characters
		if (strlen($txt)<$len) return $txt; 
		else return substr($txt,0,$len-3).'...'; 
	}
}
function dateFormat($str, $format='Y-m-d'){ // formats string or uts to a new format
	if (empty($str)) return false;
	if (strcmp(intval($str),$str)==0) $unix=$str; // unix timestamp given
	else { 
		if (preg_match('/^(\d\d\d\d)-(\d\d?)-(\d\d?)$/', $str)) $inputFormat='Y-m-d';
		elseif (preg_match('/^(\d\d?)-(\d\d?)-(\d\d\d\d)$/', $str)) $inputFormat='d-m-Y';
		elseif (preg_match('/^(\d\d?)\/(\d\d?)\/(\d\d\d\d)$/', $str)) $inputFormat='d/m/Y';
		$dateAr=dateParseFromFormat($inputFormat, $str);
		$unix = mktime(0,0,0,$dateAr['month'],$dateAr['day'],$dateAr['year']);
	}	
	return date($format, $unix); 
}

function sizeFormat($size){
	if ($size>1048576) $newSize=round($size/1048576).' MB';
	elseif ($size>1024) $newSize=round($size/1024).' KB';
	else $newSize=$size.' B';
	return $newSize;
}

function echo_array($arr,$return=false){
	$pos=print_r($arr,true);
	if ($return==true) return nl2br($pos); 
	else echo nl2br($pos);
}


function dateParseFromFormat($stFormat, $stData){
	$aDataRet = array();
	$aPieces = split('[:/.\ \-]', $stFormat);
	$aDatePart = split('[:/.\ \-]', $stData);
		foreach($aPieces as $key=>$chPiece){
			switch ($chPiece){
				case 'd':
				case 'j': $aDataRet['day'] = $aDatePart[$key]; break;
				case 'F':
				case 'M':
				case 'm':
				case 'n': $aDataRet['month'] = $aDatePart[$key]; break;
				case 'o':
				case 'Y':
				case 'y': $aDataRet['year'] = $aDatePart[$key]; break;
				case 'g':
				case 'G':
				case 'h':
				case 'H': $aDataRet['hour'] = $aDatePart[$key]; break;    
				case 'i': $aDataRet['minute'] = $aDatePart[$key]; break;
				case 's': $aDataRet['second'] = $aDatePart[$key]; break;            
			}
		}
	return $aDataRet;
}

function getIP(){
	if (!empty($_SERVER['HTTP_CLIENT_IP'])) $ip=$_SERVER['HTTP_CLIENT_IP'];							// check ip from share internet
	elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];	// to check ip is pass from proxy
	else $ip=$_SERVER['REMOTE_ADDR'];
	return $ip;
}


function set_cookie($name, $value, $time=15552000){ setcookie($name, $value, time()+$time, '/'); }		// 15552000 - about 180 days
function del_cookie($name){ setcookie($name, '', time()-3600, '/'); }

?>